﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JMT.Data;

namespace JMT.Controls.TreeNodes
{
    public class JClassTreeNode : JTreeNode
    {
        public JClassTreeNode(JClass jModel)
            : base(jModel)
        {
            IJContainer<JProperty> jPropertyContainer = jModel as IJContainer<JProperty>;
            jPropertyContainer.Register(new JContainerUpdate<JProperty>(JPropertyContainerUpdate));

            IJContainer<JMethod> jMethodContainer = jModel as IJContainer<JMethod>;
            jMethodContainer.Register(new JContainerUpdate<JMethod>(JMethodContainerUpdate));
        }

        private void JPropertyContainerUpdate(object sender, JUpdateEventArgs<JProperty> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = e.NewStartingIndex;
                JProperty jModel = e.NewItems[0] as JProperty;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jModel));
            }
        }

        private void JMethodContainerUpdate(object sender, JUpdateEventArgs<JMethod> e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                int delta = (base.JModel as IJContainer<JProperty>).ToList().Count + e.NewStartingIndex;
                JMethod jModel = e.NewItems[0] as JMethod;
                this.Items.Insert(delta, JTreeNodeFactory.Create(jModel));
            }
        }
    }
}
